package leetcode.lt100;

class Solution_43 {

	public static void main(String[] args) {
		System.out.println(new Solution_43().multiply("131", "23"));
	}

	public String multiply(String num1, String num2) {
		char[] char1 = num1.toCharArray();
		char[] char2 = num2.toCharArray();
		int l1 = char1.length;
		int l2 = char2.length;
		
		if(l1==1&&char1[0]=='0'||l2==1&&char2[0]=='0')
			return "0";

		int[] res = new int[l1 + l2];
		// 每一位分别相乘放到res数组内
		for (int i = 0; i < l1; i++) {
			for (int j = 0; j < l2; j++) {
				res[l1 + l2 - 2 - i - j] += (char1[i] - '0') * (char2[j] - '0');
			}
		}
		// 处理进位
		int current = 0;
		while (current < l1 + l2 - 1) {
			if (res[current] > 9) {
				res[current + 1] += res[current] / 10;
				res[current] %= 10;
			}
			current++;
		}

		StringBuffer sb = new StringBuffer();
		// 处理数组前0不进入结果集
		boolean start = false;
		for (int i = l1 + l2 - 1; i >= 0; i--) {
			if (start) {
				sb.append(res[i]);
			} else if (res[i] != 0) {
				start = true;
				sb.append(res[i]);
			}
		}
		return sb.toString();
	}
}